PDB

Use of ipdb within Jupyterlab

start pdb from within a script:

import pdb;pdb.set_trace()

start pdb from the commandline:

python -m pdb<file.py>

PDB / iPDB Commands

Command Description
h(elp) print available commands
h(elp) command print help about command
q(uit) quit debugger
p(rint) expr print value of expr
pp expr pretty print expr
w(here) print current position (inlcuding stack trace
l(ist) list 11 lines of code around the current line
a(rgs) print args of the current function
n(ext) execute the current statement step over
s(tep) execute and step into function
r(eturn) continue execution until the current function returns
c(ontinue) continue execution until a breakpoint is encountered
u(p) move one level up in stack trace
d(own) move one level down in stack trace
b(reak) show all breakpoints
b(reak) lineno set a breakpoint at lineno
b(reak) func set a breakpoint at the first line of a func
!stmt treat stmt as a Python statment instead of a pdb command
exit like exit debugger

In [1]:
%pdb on


Automatic pdb calling has been turned ON

In [1]:
%pdb

def pick_and_take():
    picked = numpy.random.randint(0, 1000)
    raise NotImplementedError()

pick_and_take()


Automatic pdb calling has been turned ON
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-3c4361a68642> in <module>()
      5     raise NotImplementedError()
      6 
----> 7 pick_and_take()

<ipython-input-1-3c4361a68642> in pick_and_take()
      2 
      3 def pick_and_take():
----> 4     picked = numpy.random.randint(0, 1000)
      5     raise NotImplementedError()
      6 

NameError: name 'numpy' is not defined
> <ipython-input-1-3c4361a68642>(4)pick_and_take()
      2 
      3 def pick_and_take():
----> 4     picked = numpy.random.randint(0, 1000)
      5     raise NotImplementedError()
      6 


In [3]:
import pdb;pdb.set_trace()

def func(x):
  return x + 1

for i in range(100):
  print(func(i))
  if i == 10 or i == 20:
    import pdb;pdb.set_trace()

raise Exception


--Return--
> <ipython-input-3-95a3089762f5>(1)<module>()->None
-> import pdb;pdb.set_trace()
---------------------------------------------------------------------------
BdbQuit                                   Traceback (most recent call last)
<ipython-input-3-95a3089762f5> in <module>()
----> 1 import pdb;pdb.set_trace()
      2 
      3 def func(x):
      4   return x + 1
      5 

~/anaconda3/lib/python3.6/bdb.py in trace_dispatch(self, frame, event, arg)
     53             return self.dispatch_call(frame, arg)
     54         if event == 'return':
---> 55             return self.dispatch_return(frame, arg)
     56         if event == 'exception':
     57             return self.dispatch_exception(frame, arg)

~/anaconda3/lib/python3.6/bdb.py in dispatch_return(self, frame, arg)
     97             finally:
     98                 self.frame_returning = None
---> 99             if self.quitting: raise BdbQuit
    100             # The user issued a 'next' or 'until' command.
    101             if self.stopframe is frame and self.stoplineno != -1:

BdbQuit: 

Tracer


In [1]:
def test_debug(y):
    x = 10
    # One-liner to start the debugger here.
    from IPython.core.debugger import Tracer; Tracer()()
    x = x + y
 
    for i in range(10):
        x = x+i
 
    return x
 
test_debug(10)


/home/zas/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:4: DeprecationWarning: `Tracer` is deprecated since version 5.1, directly use `IPython.core.debugger.Pdb.set_trace()`
  after removing the cwd from sys.path.
> <ipython-input-1-676cdc08eca1>(5)test_debug()
      3     # One-liner to start the debugger here.
      4     from IPython.core.debugger import Tracer; Tracer()()
----> 5     x = x + y
      6 
      7     for i in range(10):

Exiting Debugger.

Interactive Python Console

When this line is hit a python console will be invoked


In [ ]:
from IPython import embed; embed()


Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

Pixie Debugger

Doesn't work in Jupyterlab because of unsupported Javascript output.


In [1]:
import pixiedust


Pixiedust database opened successfully
Pixiedust version 1.1.15

In [2]:
%%pixie_debugger

import random
def find_max (values):
    max = 0
    for val in values:
        if val > max:
            max = val
    return max
find_max(random.sample(range(100), 10))